package com.semlab.client.mvp.home;

import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Window;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.semlab.client.app.Application;
import com.semlab.client.app.gin.Injector;
import com.semlab.client.events.ClientLoginEvent;
import com.semlab.client.fwk.Event;
import com.semlab.client.fwk.ap.Place;
import com.semlab.client.fwk.ap.mvp.BasePresenter;
import com.semlab.client.proxy.ClientProxy;
import com.semlab.client.proxy.MoviePageProxy;
import com.semlab.client.proxy.MovieProxy;
import com.semlab.client.req.RequestFactory;
import com.semlab.client.widgets.UICallback;
import com.semlab.client.widgets.WaitPanel;

public class HomePresenter extends BasePresenter<HomeMVP.View> implements
        HomeMVP.Presenter {

    private static final Logger log = Logger.getLogger(HomePresenter.class
            .getName());

    public HomePresenter(Place place, HomeMVP.View display,
                         RequestFactory factory) {
        super(place, display, factory);
        display.setPresenter(this);
    }

    @Override
    protected void onStart() {
        setViewInContainer();
        Injector.INSTANCE.getEventBus().addHandler(ClientLoginEvent.TYPE, new Event.Handler<ClientLoginEvent>() {
            @Override
            public void on(ClientLoginEvent loginEvent) {
                if (loginEvent.getClient().isImported()) {
                    display.refreshMoviesExplorer();
                }
            }
        });
    }

    @Override
    public void startImport(int friends, int movies) {
        if (Application.getClientProxy() == null) {
            Window.alert("wait");
            return;
        }
        requestFactory.dataRequest().startImport(Application.getClientProxy(), friends, movies).fire(new Receiver<Boolean>() {
            @Override
            public void onSuccess(Boolean response) {
                display.refreshMoviesExplorer();
            }
        });
    }

    @Override
    public void getAllMovies(final UICallback<MoviePageProxy> callback, int offset, int num) {
        WaitPanel.wait(true);
        requestFactory.dataRequest().getMovies(Application.getClientProxy(), offset, num)
                .fire(new Receiver<MoviePageProxy>() {
                    public void onSuccess(MoviePageProxy response) {
                        callback.callback(response);
                        WaitPanel.wait(false);
                        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                            @java.lang.Override
                            public void execute() {
                                display.showMoviesExplorer();
                            }
                        });
                    }
                });
    }

    @Override
    public void getFriendsForAMovie(MovieProxy proxy, UICallback<List<ClientProxy>> friendsCallback) {

    }

    @Override
    public void getDetailsForAMovie(MovieProxy proxy, final UICallback<MovieProxy> callback) {
        requestFactory.dataRequest().getDetailsForAMovie(Application.getClientProxy(), proxy)
                .fire(new Receiver<MovieProxy>() {
                    public void onSuccess(MovieProxy response) {
                        callback.callback(response);
                    }

                });
    }

    @Override
    public void enrichMovieData(MovieProxy movie, final UICallback<MovieProxy> detailsCallback) {
        WaitPanel.wait(true);
        requestFactory.dataRequest().enrichMovieData(movie).fire(new Receiver<MovieProxy>() {
            @Override
            public void onSuccess(MovieProxy response) {
                detailsCallback.callback(response);
                WaitPanel.wait(false);
            }

        });
    }

}
